﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BackTestProvider.Utils.cs" company="">
//   
// </copyright>
// <summary>
//   The back test provider.
// </summary>
// --------------------------------------------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using Systemathics.FrameWork;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using System.Data.SqlTypes;

namespace Systemathics.Providers
{
    public sealed partial class BackTestProvider
    {
        public static void GetMinMaxDatesDB(ref DateTime? StartDate, ref DateTime? EndDate)
        {
            using (
                var connection =
                    new SqlConnection(GlobalVariables.SqlConnectionString(GlobalVariables.TICKCAPTURES_DATABASE_NAME)))
            {
                var server = new Server(new ServerConnection(connection));
                Database db = server.Databases[GlobalVariables.TICKCAPTURES_DATABASE_NAME];
                string Query = @"SELECT MIN(FirstStartDate) FROM DATES";
                var command = new SqlCommand(Query, connection);
                SqlDataReader rdr = command.ExecuteReader();
                if (rdr.HasRows)
                {
                    rdr.Read();
                    var value = rdr.GetSqlDateTime(0);
                    if (!value.IsNull)
                        StartDate = (DateTime)value;
                }

                Query = @"SELECT MAX(LastEndDate) FROM DATES";
                command = new SqlCommand(Query, connection);
                rdr = command.ExecuteReader();
                if (rdr.HasRows)
                    if (rdr.HasRows)
                    {
                        rdr.Read();
                        var value = rdr.GetSqlDateTime(0);
                        if (!value.IsNull)
                            EndDate = (DateTime)value;
                    }

                rdr.Close();
                connection.Close();
            }
        }
        public static void GetMinMaxDatesDB(Guid Guid, ref DateTime? StartDate, ref DateTime? EndDate)
        {
            using (
                var connection =
                    new SqlConnection(GlobalVariables.SqlConnectionString(GlobalVariables.TICKCAPTURES_DATABASE_NAME)))
            {
                var server = new Server(new ServerConnection(connection));
                Database db = server.Databases[GlobalVariables.TICKCAPTURES_DATABASE_NAME];
                string Query = @"SELECT FirstStartDate , LastEndDate FROM DATES WHERE Instrument = '" + Guid + "'";
                var command = new SqlCommand(Query, connection);
                SqlDataReader rdr = command.ExecuteReader();
                if (rdr.HasRows)
                {
                    while (rdr.Read())
                    {
                        var value = rdr.GetSqlDateTime(0);
                        if (!value.IsNull)
                            StartDate = (DateTime) rdr.GetValue(0);
                        value = rdr.GetSqlDateTime(1);
                        if (!value.IsNull);
                            EndDate = (DateTime) rdr.GetValue(1); 
                    }

                    rdr.Close();
                    connection.Close();
                }
                else
                {
                    StartDate = null;
                    EndDate = null;
                }
            }
        }
        public static List<byte[]> GetDATA(Guid Guid, DateTime StartDate, DateTime EndDate)
        {
            using (
                var connection =
                    new SqlConnection(GlobalVariables.SqlConnectionString(GlobalVariables.TICKCAPTURES_DATABASE_NAME)))
            {
                var server = new Server(new ServerConnection(connection));
                Database db = server.Databases[GlobalVariables.TICKCAPTURES_DATABASE_NAME];

                var sqlstart = new SqlDateTime(StartDate.Year, StartDate.Month, StartDate.Day);
                var sqlend = new SqlDateTime(EndDate.Year, EndDate.Month, EndDate.Day);
                string Query = @"SELECT DATA FROM [" + Guid + "] WHERE '" + sqlstart + "' <= CAST(STARTDATE AS DATE) AND '" + sqlend + "' >= CAST(ENDDATE AS DATE)";
                var command = new SqlCommand(Query, connection);
                SqlDataReader rdr = command.ExecuteReader();
                var result = new List<byte[]>();
                while (rdr.Read())
                    result.Add((byte[]) rdr.GetValue(0));
                rdr.Close();
                connection.Close();
                return result;
            }
        }
        public void AddRange(Guid Guid, TickCaptures tc, ref SortedDictionary<DateTime, TickCaptureGuid> series)
        {
            foreach (var t in tc)
                series.Add(t.Key, new TickCaptureGuid(Guid, t.Value));
        }

        public class TickCaptureGuid
        {
            public Guid Guid;
            public TickCapture tc;
            public TickCaptureGuid(Guid g, TickCapture h)
            {
                Guid = g;
                tc = h;
            }
        }
    }
}